package online.hxstart.code.concurrent.chapter03;

import java.util.concurrent.TimeUnit;

/**
 * @author huxing
 * @date 19-4-24 星期三 上午9:58
 * @describe
 */
public class InterruptThreadExit {
  public static void main(String[] args) throws InterruptedException {

    Thread thread =
        new Thread() {
          @Override
          public void run() {
            System.out.println("I will start work");
            while (!isInterrupted()) {
              //System.out.println("0---------");
            }
            System.out.println("I will be exiting.");
          }
        };
    thread.start();
    TimeUnit.MILLISECONDS.sleep(1);
    System.out.println("System will be shutdown");
    thread.interrupt();

    Thread thread1 =new Thread(){
      @Override
      public void run() {
        System.out.println("I will start work");
        for (;;){
          try{
            //working
            TimeUnit.MILLISECONDS.sleep(1);
          } catch (InterruptedException e) {
            break;
          }
        }
        System.out.println("I will be exiting.");
      }
    };
    thread1.start();
    TimeUnit.MILLISECONDS.sleep(1);
    System.out.println("System will be shutdown");
    thread1.interrupt();
  }
}
